package com.summer.safe.quartz.utils;

import com.summer.safe.quartz.entity.QuartzJob;
import com.summer.safe.utils.SpringContextUtil;
import com.summer.safe.utils.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 任务调度执行类
 */
public class TaskUtils {

    /**
     * 执行目标方法
     * @param quartzJob
     * @throws Exception
     */
    public static void methodInvok(QuartzJob quartzJob)  throws Exception {
        runMethodInvoke(SpringContextUtil.getBean(quartzJob.getBeanClass()),quartzJob.getMethodName(),quartzJob.getParams());

    }
    private static void runMethodInvoke(Object bean, String methodName, String methodParams) throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {


        if (StringUtils.isNotBlank(methodParams)) {
             Method method = bean.getClass().getDeclaredMethod(methodName, String.class);
            method.invoke(bean, methodParams);
        } else {

            Method method = bean.getClass().getDeclaredMethod(methodName, String.class);
            method.invoke(bean);
        }
    }


}
